package com.ruoyi.project.goods.controller;

import cn.mabach.goods.entity.ProductAttributeCategoryEntity;
import cn.mabach.goods.dto.ProductAttributeCategoryItem;
import cn.mabach.result.PageResult;
import cn.mabach.result.RS;
import cn.mabach.utils.BeanUtilsMabach;
import com.ruoyi.project.goods.feign.ProductAttributeCategoryServiceFeign;
import com.ruoyi.project.goods.vo.ProductAttributeCategoryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 产品属性分类表
 *
 * @author chenshun
 * @email 176468159@qq.com
 * @date 2019-12-26 01:13:22
 */
@RestController
@RequestMapping("/productAttribute/category")
public class ProductAttributeCategoryController {
    @Autowired
    private ProductAttributeCategoryServiceFeign productAttributeCategoryService;

    /**
     * 列表
     */


    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('pms:productAttributeCategory:read')")
    public RS<PageResult<ProductAttributeCategoryEntity>> getList(
                                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        RS<PageResult<ProductAttributeCategoryEntity>> pageResultRS = productAttributeCategoryService.queryPage(pageNum, pageSize);

        return pageResultRS;
    }



    /**
     * 信息
     */

    @GetMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPermi('pms:productAttributeCategory:read')")
    public RS<ProductAttributeCategoryEntity> getItem(@PathVariable("id") Long id) {
        return productAttributeCategoryService.getByIdE(id);
    }





    /**
     * 保存
     */
    @PostMapping(value = "/create")
    @PreAuthorize("@ss.hasPermi('pms:productAttributeCategory:create')")
    public RS create(@RequestBody ProductAttributeCategoryEntity productAttributeCategoryEntity) {
//        ProductAttributeCategoryEntity productAttributeCategoryEntity = BeanUtilsMabach.doToDto(productAttributeCategoryParam, ProductAttributeCategoryEntity.class);
        productAttributeCategoryEntity.setAttributeCount(0);
        productAttributeCategoryEntity.setParamCount(0);

        RS rs = productAttributeCategoryService.saveE(productAttributeCategoryEntity);

        return rs;
    }



    /**
     * 修改
     */
    @PostMapping(value = "/update/{id}")
    @PreAuthorize("@ss.hasPermi('pms:productAttributeCategory:update')")
    public RS update(@PathVariable("id") Long id,
                     @Validated @RequestBody ProductAttributeCategoryParam entity,
                     BindingResult result) {
        entity.setId(id);
        ProductAttributeCategoryEntity productAttributeCategoryEntity = BeanUtilsMabach.doToDto(entity, ProductAttributeCategoryEntity.class);
        RS rs = productAttributeCategoryService.updateByIdE(productAttributeCategoryEntity);

        return rs;
    }




    /**
     * 删除
     */
    @GetMapping(value = "/delete/{id}")
    @PreAuthorize("@ss.hasPermi('pms:productAttributeCategory:delete')")
    public RS delete(@PathVariable("id") Long id) {
        RS rs = productAttributeCategoryService.removeOneById(id);

        return rs;

    }


    @PostMapping(value = "/delete/batch")
    @PreAuthorize("@ss.hasPermi('pms:productAttributeCategory:delete')")
    public RS deleteBatch(@RequestParam("ids") List<Long> ids) {
        RS rs = productAttributeCategoryService.removeByIdsE(ids);
        return rs;
    }


    @GetMapping(value = "/list/withAttr")
    public RS<List<ProductAttributeCategoryItem>> getListWithAttr() {

        return productAttributeCategoryService.getListWithAttr();
    }


}
